home *** CD-ROM | disk | FTP | other *** search
/ Graphics & Sound Program…ng Techniques for the Mac / Graphics and Sound Programming Techniques for the Mac.iso / M&T Graphics & Sound Examples / Metrowerks Versions / C02 Sound Playing / P02 Sound Channel Intro / SoundChannelIntro.c next >
Encoding:
C/C++ Source or Header  |  1995-08-05  |  2.8 KB  |  130 lines  |  [TEXT/MMCC]

  1. //____________________________________________________________
  2. //    SoundChannelIntro.c
  3. //
  4. //    Copyright © Dan Parks Sydow, 1995
  5. //    From the book:
  6. //    "Graphics and Sound Programming Techniques for the Mac",
  7. //    M&T Books, 1995
  8.  
  9.  
  10. //____________________________________________________________
  11.  
  12. #include <Sound.h>
  13.  
  14.  
  15. //____________________________________________________________
  16.  
  17. void           InitializeToolbox( void );
  18. SndChannelPtr  OpenOneSynchSoundChannel( void );
  19. OSErr          DisposeOneSoundChannel( SndChannelPtr );
  20. OSErr          PlaySoundResourceSynch( SndChannelPtr, short );
  21.  
  22.  
  23. //____________________________________________________________
  24.  
  25. #define    rPoliceSiren        9000
  26.  
  27.  
  28. //____________________________________________________________
  29.  
  30. void  main( void )
  31. {
  32.    NumVersion     theSndMgrVers;
  33.    short          theResID;
  34.    OSErr          theError;
  35.    SndChannelPtr  theChannel;
  36.          
  37.    InitializeToolbox();
  38.  
  39.    theSndMgrVers = SndSoundManagerVersion();   
  40.    if ( theSndMgrVers.majorRev < 3 )
  41.       ExitToShell();
  42.    
  43.    theChannel = OpenOneSynchSoundChannel();
  44.    if ( theChannel == nil )
  45.       ExitToShell();
  46.   
  47.    theResID = rPoliceSiren; 
  48.    theError = PlaySoundResourceSynch( theChannel, theResID );
  49.    if ( theError != noErr )
  50.       ExitToShell();
  51.       
  52.    theError = DisposeOneSoundChannel( theChannel );
  53.    if ( theError != noErr )
  54.       ExitToShell();
  55. }
  56.  
  57.  
  58. //____________________________________________________________
  59.  
  60. SndChannelPtr  OpenOneSynchSoundChannel( void )
  61. {
  62.    SndChannelPtr  theChannel;   
  63.    OSErr          theError;
  64.    
  65.    theChannel = nil;
  66.    theError = SndNewChannel( &theChannel, 0, 0, nil );
  67.    
  68.    if ( theError != noErr )
  69.    {
  70.       DisposePtr( (Ptr)theChannel );
  71.       theChannel = nil;
  72.    }
  73.       
  74.    return ( theChannel );
  75. }
  76.  
  77.  
  78. //____________________________________________________________
  79.  
  80. OSErr  DisposeOneSoundChannel( SndChannelPtr theChannel )
  81. {
  82.    OSErr  theError;
  83.    
  84.    theError = SndDisposeChannel( theChannel, true );
  85.    DisposePtr( (Ptr)theChannel );
  86.    
  87.    return ( theError );
  88. }
  89.  
  90.  
  91. //____________________________________________________________
  92.  
  93. OSErr  PlaySoundResourceSynch( SndChannelPtr theChannel, short theResID )
  94. {
  95.    Handle  theHandle;
  96.    OSErr   theError;
  97.    
  98.    theHandle = GetResource( 'snd ', theResID );
  99.    
  100.    if ( theHandle == nil )
  101.    {   
  102.       return ( resProblem );
  103.    }
  104.    else
  105.    {
  106.       HLock( theHandle );
  107.          theError = SndPlay( theChannel, (SndListHandle)theHandle, false );
  108.       HUnlock( theHandle );
  109.    
  110.       ReleaseResource( theHandle );
  111.  
  112.       return ( theError );
  113.    }
  114. }
  115.  
  116.  
  117. //____________________________________________________________
  118.  
  119. void  InitializeToolbox( void )
  120. {
  121.    InitGraf( &qd.thePort );
  122.    InitFonts();
  123.    InitWindows();
  124.    InitMenus();
  125.    TEInit();
  126.    InitDialogs( 0L );
  127.    FlushEvents( everyEvent, 0 );
  128.    InitCursor();
  129. }
  130.